package com.ruoyi.flowable.listener.scrap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.equ.domain.EquScrap;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.flowable.listener.asset.AssetListener_2.getFileList;

/**
 * 报废-4-1监听：环保设备
 */
@Slf4j
@Component("scrapListener_4_1")
public class ScrapListener_4_1 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            HashMap<String, Object> equMap = new HashMap<>();
            String code7 = execution.getVariable("code7", String.class);
            List<String> license7 = getFileList(execution, "license7");
            equMap.put("处置单位资质证明编号", code7);
            equMap.put("处置单位营业执照", license7);
            //赋值
            EquScrap equScrap = execution.getVariable("equScrap", EquScrap.class);

            HashMap<String, Object> disposalInfoMap = new HashMap<>();
            disposalInfoMap.put("环保设备处置", equMap);
            String disposalInfo = objectMapper.writeValueAsString(disposalInfoMap);

            //附件链接
            HashMap<String, Object> fileUrlsMap = objectMapper.readValue(equScrap.getFileUrls(), new TypeReference<HashMap<String, Object>>() {});
            fileUrlsMap.put("处置单位营业执照", license7);
            String fileUrls = objectMapper.writeValueAsString(fileUrlsMap);

            equScrap.setDisposalInfo(disposalInfo);
            equScrap.setFileUrls(fileUrls);

            execution.setVariable("equScrap", equScrap);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
